#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple example of using hrtimers");

static struct hrtimer my_hrtimer;
static ktime_t kt_periode;

// 定时器回调函数
enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
    printk(KERN_INFO "Timer callback function called.\n");
    // 重新启动定时器
    hrtimer_forward_now(timer, kt_periode);
    return HRTIMER_RESTART;
}

// 初始化模块
static int __init example_init(void)
{
    printk(KERN_INFO "HRTimer module init.\n");

    // 设置定时器周期，例如 1 秒
    kt_periode = ktime_set(1, 0); // 1 秒，0 纳秒

    // 初始化高分辨率定时器
    hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    my_hrtimer.function = &timer_callback;

    // 启动定时器
    hrtimer_start(&my_hrtimer, kt_periode, HRTIMER_MODE_REL);

    return 0;
}

// 清理模块
static void __exit example_exit(void)
{
    int ret;

    ret = hrtimer_cancel(&my_hrtimer);
    if (ret)
        printk(KERN_INFO "The timer was still in use...\n");
    printk(KERN_INFO "HR Timer module uninstalling\n");
}

module_init(example_init);
module_exit(example_exit);